package cn.bonoon.controllers;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.bonoon.util.VerifyCodeHelper;

@Controller
@RequestMapping
public class CommonController {
	protected final static char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
	
	@RequestMapping("comm/verifycode.b")
	public void verifycode(HttpServletRequest request, HttpServletResponse response, Integer type) throws Exception {
		BufferedImage img = new BufferedImage(80, 22, BufferedImage.TYPE_INT_RGB);
		// 得到该图片的绘图对象
		Graphics g = img.getGraphics();
		Random r = new Random();
		g.setColor(new Color(236, 233, 216));
		// 填充整个图片的颜色
		g.fillRect(0, 0, 80, 22);
		// 向图片中输出数字和字母
		StringBuffer sb = new StringBuffer();
		int index, len = ch.length;
		for (int i = 0; i < 5; i++) {
			index = r.nextInt(len);
			g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255)));
			g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));// 输出的
																		// 字体和大小
			g.drawString("" + ch[index], (i * 15) + 3, 18);// 写什么数字，在图片 的什么位置画
			sb.append(ch[index]);
		}
		VerifyCodeHelper.setCode(request, type, sb.toString());
		ImageIO.write(img, "JPG", response.getOutputStream());
	}
	
	@RequestMapping("comm/notright.b")
	public String notright(){
		return "notright";
	}

	@RequestMapping("cas")
	public String cas(){
		System.out.println("in cas controller");
	return "redirect:/s";	
	}
}
